﻿using CardMonitor.Model;
using CardMonitor.Utils;
using CardMonitor.Windows;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Pages
{
    /// <summary>
    /// Print.xaml 的交互逻辑
    /// </summary>
    public partial class PrintPage : Page
    {
        MainWindow mainWindow = null;
        SynchronizationContext _syncContext = null;
        List<BillModel> items = new List<BillModel>();
        int m_iInit1 = -1;
        //int m_iStatus1 = -1;
        int m_iStatus = -1;
        public PrintPage()
        {
            InitializeComponent();
        }
        
        public PrintPage(List<BillModel> bills,MainWindow _win)
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;

            mainWindow = _win;
            
            items = bills;
            listView.ItemsSource = items;
        }
        public void loadData()
        {
            items.Add(new BillModel());
            items.Add(new BillModel());
            items.Add(new BillModel());
            items.Add(new BillModel());
        }
        private void goIndex(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).GoBack();
        }

        private void Button_Click_PrintAccount(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var item = btn.DataContext as BillModel;

            long id = item.Id;
            string t = "1";

            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;
            JObject paramJSON = new JObject();
            //paramJSON.Add("t", t);
            paramJSON.Add("id", id);
            httpPostWindow.setParamObject(paramJSON);
            httpPostWindow.setOperation(HttpPostWindow.OperationType.queryBillPrintCount.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = "结算失败,请到人工窗口办理";
                showMessage(message);
                return;
            }
            JObject resultObj = httpPostWindow.getResultObject();
            if (null == resultObj)
            {
                showMessage("发生错误");
                return;
            }
            string printCount = resultObj["count"].ToString();
            if (!"1".Equals(printCount))
            {
                showMessage("超打印次数,请到人工窗口");
                return;
            }
            if (App.getConfig().IfPrint.Equals("0"))
            {
                PrintWindow printWindow = new PrintWindow();
                printWindow.setBillMode(item);
                if ("1".Equals(item.Type))
                {
                    printWindow.setTemplete("tplSaleAccountS","1");
                }
                else
                {
                    printWindow.setTemplete("tplBuyAccountS","1");
                }
                printWindow.Owner = mainWindow;
                printWindow.ShowDialog();
            }
            
        }

        private void Button_Click_PrintOut(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var item = btn.DataContext as BillModel;

            long id = item.Id;
            string t = "2";

            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;
            JObject paramJSON = new JObject();
            paramJSON.Add("t", t);
            paramJSON.Add("id", id);
            httpPostWindow.setParamObject(paramJSON);
            httpPostWindow.setOperation(HttpPostWindow.OperationType.queryBillPrintCount.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = "结算失败,请到人工窗口办理";
                showMessage(message);
                return;
            }
            JObject resultObj = httpPostWindow.getResultObject();
            if (null == resultObj)
            {
                showMessage("发生错误");
                return;
            }
            string printCount = resultObj["count"].ToString();
            if (!"1".Equals(printCount))
            {
                showMessage("超打印次数,请到人工窗口");
                return;
            }

            PrintWindow printWindow = new PrintWindow();
            printWindow.setBillMode(item);
            if ("1".Equals(item.Type))
            {
                printWindow.setTemplete("tplSaleOutS","1");
            }
            else
            {
                printWindow.setTemplete("tplBuyOutS","1");
            }
            printWindow.Owner = mainWindow;
            printWindow.ShowDialog();
        }

        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }

        private void Button_Click_PrintWeightTicket(object sender, RoutedEventArgs e)//榜单打印
        {
            //int selectedIndex = listView.SelectedIndex;
            //BillModel item = new BillModel();
            //BillModel billmodel = (BillModel)listView.SelectedItem;
            var btn = sender as Button;
            var item = btn.DataContext as BillModel;
            MsPrinterUtils.SetUsbportauto();//自动配置USB端口
            m_iInit1 = MsPrinterUtils.SetInit();//初始化打印机
            m_iStatus = MsPrinterUtils.GetStatus();//获取打印机状态
            if (m_iStatus == 7)
            {
                showMessage("打印机缺纸,请联系工作人员");
            }

            else if (m_iStatus == 0 || m_iStatus == 8)
            {
                if (item.hasPrint == "0")
                {
                    PrintWindow printWindow = new PrintWindow();
                    printWindow.setBillMode(item);


                    if ("0".Equals(item.Type)|| "5".Equals(item.Type)|| "4".Equals(item.Type))
                    {
                        printWindow.setTemplete("tplWeighsale", "1");
                    }
                    else
                    {
                        printWindow.setTemplete("tplWeighPurchase", "1");
                    }
                    printWindow.Owner = mainWindow;
                    bool result = (bool)printWindow.ShowDialog();
                    if (!result)
                    {
                        showMessage("打印失败，请联系工作人员");
                    }
                    else
                    {
                        Index index = new Index(mainWindow);

                        NavigationService.GetNavigationService(this).Navigate(index);
                    }
                }
                else
                {
                    showMessage("超打印次数,请到人工窗口");
                }
            }
            else
            {
                showMessage("打印机异常" + m_iStatus);
            }
           
            //var item = btn.DataContext as BillModel;
            //if (btn != null )
            //{
            //    var listViewItem = btn.Tag as ListViewItem;
            //    if (listViewItem != null)
            //    {
            //         item = listViewItem.Content as BillModel;
            //    }
            //}
            //var item = billmodel;

            //long id = item.Id;
            //string t = "2";

            //HttpPostWindow httpPostWindow = new HttpPostWindow();
            //httpPostWindow.Owner = mainWindow;
            //JObject paramJSON = new JObject();
            //paramJSON.Add("t", t);
            //paramJSON.Add("id", id);
            //httpPostWindow.setParamObject(paramJSON);
            //httpPostWindow.setOperation(HttpPostWindow.OperationType.queryBillPrintCount.ToString());
            //bool result = (bool)httpPostWindow.ShowDialog();
            //if (!result)
            //{
            //    string message = "结算失败,请到人工窗口办理";
            //    showMessage(message);
            //    return;
            //}
            //JObject resultObj = httpPostWindow.getResultObject();
            //if (null == resultObj)
            //{
            //    showMessage("发生错误");
            //    return;
            //}
            //string printCount = resultObj["count"].ToString();
            //if (!"1".Equals(printCount))
            //{
            //    showMessage("超打印次数,请到人工窗口");
            //    return;
            //}

           

        }

        private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
